package com.cg.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cg.grpc.Greeting;
import com.cg.grpc.HelloWorldServiceGrpc;
import com.cg.grpc.User;
import com.cg.grpc.UserVO;
import com.cg.mapper.UserMapper;
import com.cg.repository.UserRepository;
import com.cg.util.UserMappers;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserRepositoryImpl extends ServiceImpl<UserMapper,UserVO> implements UserRepository {

    @Autowired
    private UserMapper userMapper;

    @GrpcClient("spring-grpc-server")
    private HelloWorldServiceGrpc.HelloWorldServiceBlockingStub helloWorldServiceStub;

    @Override
    public String findUserNameById(Integer id) {
        QueryWrapper<UserVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        UserVO u = baseMapper.selectOne(queryWrapper);
        User user = UserMappers.INSTANCE.map(u);
//        Greeting greeting = helloWorldServiceStub.sayHello(User.newBuilder().setUsername("che").build());
        Greeting greeting = helloWorldServiceStub.sayHello(user);
        return greeting.getMessage();
    }
}
